package com.bluejie.bluej.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Executor;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpUtil {
    //http://blog.csdn.net/zmx729618/article/details/51799886
    private static Logger log = LoggerFactory.getLogger(HttpUtil.class);

    private Executor executor = null;


    public HttpUtil(){
        executor = Executor.newInstance(getHttpClient());
    }

    private  HttpClient getHttpClient(){
        HttpClient httpClient =null;
        SSLContext sslContext = null;
        SSLConnectionSocketFactory sslConnectionSocketFactory = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
            sslConnectionSocketFactory  = new SSLConnectionSocketFactory(sslContext,new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).setConnectionManagerShared(true).build();
        }catch (Exception e){
            log.error("sslContext.error",e);
        }
        return  httpClient;
    }


}
